package router

import (
	"bike-vb/handler"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Cors() gin.HandlerFunc {

	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}

func Init() *gin.Engine {
	r := gin.Default()
	r.Use(Cors())
	r.Static("/static", "./static")
	r.POST("/user/login", handler.UserLogin)

	r.POST("/user/userRoleAdd", handler.UserRoleAdd)
	r.GET("/user/userRoleList", handler.UserRoleList)

	r.POST("/user/RoleNodeAdd", handler.RoleNodeAdd)
	r.GET("/user/RoleNodeList", handler.RoleNodeList)

	r.GET("/user/list", handler.UserList)
	r.GET("/user/userDetail", handler.UserDetail)

	r.POST("/users/logins", handler.Login)
	r.GET("/users/userRepoOrderList", handler.UserRepoOrderList)
	r.GET("/users/userRepoOrderDetail", handler.UserRepoOrderDetail)

	//仓库
	r.POST("/repo/repoAdd", handler.RepoAdd)
	r.POST("/repo/repoOrderAdd", handler.RepoOrderAdd)

	r.GET("/repo/repoOrderList", handler.RepoOrderList)
	r.GET("/repo/repoOrderDetail", handler.RepoOrderDetail)

	//车辆
	r.POST("/car/carAdd", handler.CarAdd)
	r.POST("/repo/carOrderAdd", handler.CarOrderAdd)

	r.GET("/car/carOrderList", handler.CarOrderList)
	r.GET("/car/carOrderDetail", handler.CarOrderDetail)

	//商品
	r.GET("/good/goodOrderList", handler.GoodOrderList)
	r.GET("/good/goodOrderDetail", handler.GoodOrderDetail)

	//配送订单
	r.GET("/per/perOrderList", handler.PerOrderList)
	r.GET("/per/perOrderDetail", handler.PerOrderDetail)

	//车辆交易订单
	r.GET("/bigcar/bigcarOrderList", handler.BigcarOrderList)
	r.GET("/bigcar/bigcarOrderDetail", handler.BigcarOrderDetail)
	return r
}
